package com.starbaba.stat.view;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.base.common.database.BaseDao;
import com.base.common.log.LogService;
import com.opensymphony.xwork2.ModelDriven;
import com.starbaba.core.utils.PropertyParamMapBuilder;
import com.starbaba.core.utils.Struts2Utils;
import com.starbaba.core.utils.json.JsonUtils;
import com.starbaba.core.web.page.PageResponse;
import com.starbaba.core.web.struts2.BaseAction;
import com.starbaba.model.dictionary.Constant;
import com.starbaba.stat.model.StatWeeklyAwardModel;

/**
 * 每周大奖参与汇总统计
 * 
 * @author Administrator
 *
 */
@SuppressWarnings({ "serial", "rawtypes", "deprecation" })
public class StatWeeklyAwardAction extends BaseAction implements ModelDriven<StatWeeklyAwardModel> {

	private StatWeeklyAwardModel model;

	public StatWeeklyAwardAction() {

	}

	@SuppressWarnings("unchecked")
	public void queryWeeklyAwardStatistics() {

		BaseDao dao = new BaseDao(Constant.POOL_LIFE);
		paramsMap = PropertyParamMapBuilder.buildFromHttpRequest(Struts2Utils.getRequest(),
				paramsMap);
		parsePageRequest();
		
		try {
			String startTime = (String)paramsMap.get("startTime");
			String endTime = (String)paramsMap.get("endTime");
			
			int start = pageRequest.getIDisplayStart();
			int length = pageRequest.getIDisplayLength();
			
			StringBuffer statSql = new StringBuffer();
			statSql.append("select a.id,a.begin_date,a.end_date,a.name,a.cost_point,b.join_num, c.user_num from (");
			statSql.append("select id, begin_date, end_date, name, cost_point from weekly_awards_config) a ");
			statSql.append("LEFT JOIN (select activity_id, count(id) join_num from weekly_awards_record GROUP BY activity_id) b on a.id = b.activity_id ");
			statSql.append("LEFT JOIN (select activity_id, count(DISTINCT userid) user_num from weekly_awards_record GROUP BY activity_id) c on a.id = c.activity_id ");
			statSql.append("where 1=1 ");
			if(StringUtils.isNotEmpty(startTime)){
				statSql.append(" and begin_date >= '" + startTime + "'"); 
			}
			
			if(StringUtils.isNotEmpty(endTime)){
				statSql.append(" and end_date <= '" + endTime + "'"); 
			}
			
			statSql.append(" order by a." + pageRequest.getOrderby() + " ");
			statSql.append(pageRequest.getOrder());
			
			String sql = statSql.toString() + " LIMIT " + start + ", " + length;
		
			List<StatWeeklyAwardModel> list = dao.list(sql, StatWeeklyAwardModel.class);
		    
			long nowStamp = new Date().getTime();
			String getpuuv = "select click_uv detail_uv, click_pv detail_pv, join_uv submit_uv, join_pv submit_pv from stat.stat_weekly_award where activity_id = ? order by date desc limit 1";
			for (StatWeeklyAwardModel model : list) {
				
				StatWeeklyAwardModel pvuv = dao.get(getpuuv, StatWeeklyAwardModel.class, model.getId());
				model.setDetail_uv(pvuv.getDetail_uv());
				model.setDetail_pv(pvuv.getDetail_pv());
				
				model.setSubmit_uv(pvuv.getSubmit_uv());
				model.setSubmit_pv(pvuv.getSubmit_pv());
				
				int lottery_status = 0;
				long endTimeStamp = 0l;
				if (model.getEnd_date() != null) {
					endTimeStamp = model.getEnd_date().getTime();
				}
				if (nowStamp > endTimeStamp) {
					lottery_status = 1; // 已结束
				}
				model.setLottery_status(lottery_status);
			}

			PageResponse response = new PageResponse();
			response.setAaData(list);
			List<StatWeeklyAwardModel> list2 = dao.list(statSql.toString(), StatWeeklyAwardModel.class);
			response.setITotalDisplayRecords(list2.size());
			response.setITotalRecords(list2.size());
			response.setSEcho(pageRequest.getSEcho());
			sendJsonMessage(JsonUtils.toJson(response, null, true, null, false));
		} catch (Exception e) {
			LogService.error("getGeneraldata", e);
		}
	}

	@Override
	public StatWeeklyAwardModel getModel() {
		if (model == null) {
			model = new StatWeeklyAwardModel();
		}
		return model;
	}
}